<template>
  <div>
    <van-sticky>
      <van-nav-bar left-arrow title="黑马优购" @click-left="$router.go(-1)" />
    </van-sticky>
    <!-- 商品的轮播图片 -->
    <van-image-preview v-model="show" :startPosition="picIndex" :images="images" @change="onChange"></van-image-preview>
    <van-swipe :autoplay="3000" indicator-color="white">
      <van-swipe-item
        @click="clickHandle(index)"
        v-for="(item,index) in goodsInfo.pics"
        :key="index"
      >
        <img :src="item.pics_big" alt />
      </van-swipe-item>
    </van-swipe>
    <!-- 商品信息区域 -->
    <!-- 价格,名称,收藏图标 -->
    <div class="box1">
      <div class="price">
        <p>￥{{ goodsInfo.goods_price }}</p>
      </div>
      <div class="title">
        <div class="left">{{ goodsInfo.goods_name }}</div>
        <div class="right">
          <van-icon name="star-o" />
          <div>收藏</div>
        </div>
      </div>
      <div class="kd">
        <span>快递: 免运费</span>
      </div>
    </div>
    <div class="line"></div>
    <!-- 促销,已选 -->
    <div class="box2">
      <div>
        <span>促销：</span>
        <span class="val">满300元减290</span>
      </div>
      <div>
        <span>已选：</span>
        <span class="val">黑色/6XL/1件</span>
      </div>
    </div>
    <div class="line"></div>
    <!-- 配送地址 -->
    <div class="box3" @click="$router.push('/pages/address_list')">
      <span>送至：</span>
      <span class="val"></span>
    </div>
    <div class="line"></div>
    <!-- 图文详情区域 -->
    <van-tabs v-model="active">
      <van-tab title="图文详情">
        <!-- 使用v-html解析 html 格式的字符串 -->
        <div v-html="goodsInfo.goods_introduce"></div>
      </van-tab>
      <van-tab class="attrs" title="规格参数">
        <van-row v-for="(item,index) in goodsInfo.attrs" :key="index">
          <van-col :span="10">{{ item.attr_name }}</van-col>
          <van-col :span="14">{{ item.attr_vals }}</van-col>
        </van-row>
      </van-tab>
    </van-tabs>

    <!-- 商品导航区域 -->
    <van-goods-action>
      <van-goods-action-icon icon="chat-o" text="客服" />
      <van-goods-action-icon icon="cart-o" text="购物车" />
      <van-goods-action-icon icon="shop-o" text="店铺"/>
      <van-goods-action-button type="warning" text="加入购物车" />
    </van-goods-action>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goodsInfo: {},
      id: null,
      show: false,
      images: [],
      picIndex: 0,
      active: 0
    };
  },
  created() {
    this.id = this.$route.params.id;
    this.getGoodsInfo();
  },
  methods: {
    clickHandle(index) {
      this.picIndex = index;
      this.show = true;
    },
    onChange() {},
    async getGoodsInfo() {
      const { data: res } = await this.$http.get(
        "/goods/detail?goods_id=" + this.id
      );
      if (res.meta.status !== 200) {
        return this.$toast.fail("获取商品信息失败");
      }
      this.goodsInfo = res.message;
      this.goodsInfo.pics.forEach(item => {
        this.images.push(item.pics_big);
      });
    }
  }
};
</script>

<style lang="less" scoped>
.van-swipe {
  img {
    width: 375px;
    height: 375px;
  }
}
.box1 {
  padding: 5px;
  .price {
    p {
      font-size: 20px;
      color: red;
    }
  }
  .title {
    display: flex;
    justify-content: space-between;
    .left {
      font-size: 14px;
    }
    .right {
      font-size: 12px;
      margin-left: 5px;
      text-align: center;
      width: 100px;
    }
  }
  .kd {
    margin-top: 5px;
    span {
      font-weight: 800;
      font-size: 14px;
      color: #666666;
    }
  }
}
.line {
  background-color: #efefef;
  height: 6px;
}
.val {
  color: #666;
}
.box2 {
  font-size: 12px;
  div {
    margin: 10px;
  }
}
.box3 {
  font-size: 12px;
  padding: 10px;
}
.attrs {
  .van-row {
    border-top: 1px solid #ccc;
  }
  :nth-child(1) {
    border-right: 1px solid #ccc;
  }
  .van-col {
    font-size: 12px;
    line-height: 66px;
    text-align: center;
  }
}
.van-goods-action {
    z-index: 99;
}
</style>